package com.zhongying.customer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhongying.jf.service.UserCheckService;
import com.zhongying.sys.utils.PageUtils;
import com.zhongying.sys.utils.R;

/**
 * @author 周 浩
 * @email zhou_eric90@163.com
 * @date 2018年4月3日 上午10:53:34
 * @描述 换卡记录
 */
@Controller
@RequestMapping("/switch_card")
public class BindCardController {

	@Autowired
	UserCheckService userCheckService;

	static final Map<String, String> BANK_MAPPER = new HashMap<>();
	static {
		BANK_MAPPER.put("100", "工商银行");
		BANK_MAPPER.put("101", "农业银行");
		BANK_MAPPER.put("102", "招商银行");
		BANK_MAPPER.put("103", "建设银行");
		BANK_MAPPER.put("104", "北京银行");
		BANK_MAPPER.put("105", "北京农村商业银行");
		BANK_MAPPER.put("106", "中国银行");
		BANK_MAPPER.put("107", "交通银行");
		BANK_MAPPER.put("108", "民生银行");
		BANK_MAPPER.put("109", "上海银行");
		BANK_MAPPER.put("110", "渤海银行");
		BANK_MAPPER.put("111", "光大银行");
		BANK_MAPPER.put("112", "兴业银行");
		BANK_MAPPER.put("113", "中信银行");
		BANK_MAPPER.put("114", "浙商银行");
		BANK_MAPPER.put("115", "广发银行");
		BANK_MAPPER.put("116", "东亚银行");
		BANK_MAPPER.put("117", "华夏银行");
		BANK_MAPPER.put("118", "杭州银行");
		BANK_MAPPER.put("119", "南京银行");
		BANK_MAPPER.put("120", "平安银行");
		BANK_MAPPER.put("121", "邮储银行");
		BANK_MAPPER.put("122", "深发银行");
		BANK_MAPPER.put("123", "浦发银行");
		BANK_MAPPER.put("124", "上海农村商业银行");
		BANK_MAPPER.put("125", "恒丰银行");
		BANK_MAPPER.put("126", "厦门银行");
	}

	/**
	 * 查看自动投标设置
	 * 
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @param userCode
	 *            用户Id
	 * @param page
	 *            当前页码
	 * @param limit
	 *            每页条数
	 * @return
	 */
	@RequestMapping("/list_records")
	@ResponseBody
	public R listAutoBorrowRecords(String userCode, Integer page, Integer limit) {
		if (userCode == null || page == null || page <= 0 || limit == null || limit <= 0) {
			return R.error(-1, "查询参数不合法");
		}
		int total = userCheckService.selectAllUserOpenBankCardDataCount(userCode);
		Map<String, Object> inMap = new HashMap<>();
		inMap.put("user_code", userCode);
		int start = 0;
		if (page == 1) {
			start = (page - 1) * limit;
		} else {
			start = (page - 1) * limit - 1;
			inMap.put("size", limit + 1);
		}

		inMap.put("start", start);

		List<Map<String, String>> result = userCheckService.selectAllUserOpenBankCardData(inMap);
		List<Map<String, String>> list = new ArrayList<>();
		if (result != null && result.size() > 0) {
			Collections.sort(result, new Comparator<Map<String, String>>() {
				public int compare(Map<String, String> o1, Map<String, String> o2) {
					String date1 = o1.get("createTime");
					String date2 = o2.get("createTime");
					return date1.compareTo(date2);
				}
			});
			for (int i = 0, len = result.size(); i < len; i++) {
				if (page != 1 && i == 0) {
					continue;
				}
				Map<String, String> data = result.get(i);
				Map<String, String> record = new HashMap<>();
				String orderNo = data.get("orderNo");
				String bankNo = data.get("bankNo");
				String bankName = BANK_MAPPER.get(bankNo);
				String createTime = data.get("createTime");
				String bankCardNo = data.get("bankCardNo");
				if (i == 0) {
					record.put("before", "—");
				} else {
					Map<String, String> last = result.get(i - 1);
					String lastBankCardNo = last.get("bankCardNo");
					StringBuilder before = new StringBuilder();
					before.append(lastBankCardNo.substring(0, 4));
					int ss = lastBankCardNo.length() - 8;
					for (int j = 0; j < ss; j++) {
						before.append("*");
					}
					before.append(lastBankCardNo.substring(lastBankCardNo.length() - 4, lastBankCardNo.length()));
					before.append("		" + BANK_MAPPER.get(last.get("bankNo")));
					record.put("before", before.toString());
				}
				StringBuilder after = new StringBuilder();
				after.append(bankCardNo.substring(0, 4));
				int ss = bankCardNo.length() - 8;
				for (int j = 0; j < ss; j++) {
					after.append("*");
				}
				after.append(bankCardNo.substring(bankCardNo.length() - 4, bankCardNo.length()));
				after.append("		" + bankName);
				record.put("after", after.toString());
				record.put("orderNo", orderNo);
				record.put("createTime", createTime);
				list.add(record);
			}
		}
		PageUtils pageUtil = new PageUtils(list, total, limit, page);
		return R.ok().put("page", pageUtil);
	}
}
